package com.eam.action;

import java.util.List;
import java.util.Map;

import com.eam.bean.Department;
import com.eam.service.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DepartmentAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private Department department;
	private DepartmentService departmentService;
	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public String save() {
		System.out.println("name:" + department.getName());
		boolean save = departmentService.save(department);
		if (save) {
			query();
			return SUCCESS;
		}
		return INPUT;

	}

	public String delete() {
		
		boolean delete = departmentService.delete(department);
		if (delete) {
			query();
			return SUCCESS;
		}
		return INPUT;

	}

	public String updatePre() {
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		System.out.println("id:" + department.getDepartmentId());
		Department dep = departmentService.updatePre(department);
		System.out.println("name:" + department.getName());
		session.put("department", dep);
		return SUCCESS;

	}

	public String update() {
		
		boolean update = departmentService.update(department);
		if (update) {
			query();
			return SUCCESS;
		}
		return INPUT;

	}
	public void query(){
		List<Department> list = departmentService.query();
		if (0 != list.size()) {
			ActionContext context = ActionContext.getContext();
			Map<String, Object> session = context.getSession();
			session.put("departmentList", list);
		}
	}

}
